package com.opentd.npc;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;

import com.opentd.game.Game;
import com.opentd.graphics.Drawable;

public class Tower implements Drawable{
	private int mapX, mapY, type;
	private int pixelX, pixelY;
	private int damage;
	private int mapRange;
	private int waitTime;
	private String imgUrl;
	private boolean isSelected;
	public Point center;
	
	int rangePixelWidth;
	int rangePixelHeight;
	int rangePixelX;
	int rangePixelY;
	
	
	public Tower(int x, int y, int type){
		mapX = x;
		mapY = y;
		pixelX = Game.Block.WIDTH * mapX;
		pixelY = Game.Block.HEIGHT * mapY;
		center = new Point(pixelX + (Game.Block.WIDTH /2), pixelY + (Game.Block.HEIGHT /2));
		this.type = type;
		switch(type){
		case 1: //Weak, but long range
			damage = 1;
			mapRange = 4;
			waitTime = 200;
			imgUrl = "dat/img/tower_arrow.png";
			break;
		case 2://Strong, short range
			damage = 5;
			mapRange = 2;
			waitTime = 500;
			imgUrl = "dat/img/tower_cannon.png";
			break;
		}
		rangePixelWidth = (mapRange * 2) * Game.Block.WIDTH;
		rangePixelHeight = (mapRange * 2) * Game.Block.HEIGHT;
		rangePixelX = ((mapX - mapRange + 1) * Game.Block.WIDTH) - (Game.Block.WIDTH / 2);
		rangePixelY = ((mapY - mapRange + 1) * Game.Block.HEIGHT) - (Game.Block.WIDTH / 2);
	}

	@Override
	public void draw(Graphics g) {
		if(type == 1){
			g.setColor(Color.BLUE);
		}else{
			g.setColor(Color.RED);
		}
		Enemy e;
		if((e = nearestEnemy()) != null){
			g.drawLine(center.x, center.y, e.center.x, e.center.y);
		}
		
		//TODO Draw image of tower instead, after the tower images are created.
		g.fillOval(pixelX, pixelY, Game.Block.WIDTH, Game.Block.HEIGHT);
		g.setColor(Color.BLACK);
		
		if(isSelected){
			drawRange(g);
			drawMenu(g);
		}
	}
	
	private void drawRange(Graphics g){
		g.drawOval(rangePixelX, rangePixelY, rangePixelWidth, rangePixelHeight);
	}
	
	public void drawMenu(Graphics g){
		//TODO
	}

	private int waitCounter = 0;
	public void logic(){
		Enemy e = nearestEnemy();
		if(e != null){
			if(waitCounter >= waitTime){
				e.damage(damage);
				waitCounter = 0;
			}else{
				waitCounter ++;
			}
		}
	}
	
	
	private Enemy nearestEnemy(){
		//System.out.println("Finding nearest enemy");
		for(Enemy e : Game.selectedLevel.waves.elementAt(Game.selectedLevel.currentWave).enemies){
			if(e.inGame){
				if(isInRange(e)){
					return e;
				}
			}
		}
		//System.out.println("No enemy found");
		return null;
	}
	
	private boolean isInRange(Enemy e){
		//System.out.println("Enemy center = x:" + enemyCenter.x + " y:" + enemyCenter.y);
		int distance = (int) Math.floor(Math.sqrt(Math.pow(e.center.x - center.x, 2) +  Math.pow(e.center.y - center.y, 2)));
		//System.out.println("Distance = " + distance);
		if(distance <= (mapRange * Game.Block.HEIGHT))
			return true;
		return false;
	}
	
	//System.out.println("Panel width: " + Game.panel.getWidth() + " Level width: " + Game.selectedLevel.map.width);
	int postRenderBlockWidth = Game.panel.getWidth() / Game.selectedLevel.map.width;
	//System.out.println("Panel height: " + Game.panel.getHeight() + " Level height: " + (Game.panel.getHeight() - Game.Levels.MAP_HEIGHT_IN_PIXELS));
	int postRenderBlockHeight = (Game.panel.getHeight() - Game.Levels.MAP_HEIGHT_IN_PIXELS) / Game.selectedLevel.map.height;
	public boolean contians(Point p){
		int postRenderX = mapX * postRenderBlockWidth;
		int postRenderY = mapY * postRenderBlockHeight;
		//System.out.println("x: " + postRenderX + " y: " + postRenderY + " width: " + postRenderBlockWidth + " height: " + postRenderBlockHeight);
		return new Rectangle(postRenderX, postRenderY, postRenderBlockWidth, postRenderBlockHeight).contains(p);
	}
	
	public void setSelected(boolean selected){
		isSelected = selected;
	}

}
